//初始化整个游戏的精灵，作为游戏开始的入口
import {ResourceLoader} from "./js/base/ResourceLoader.js";
import {BackGroundStart} from "./js/runtime/BackGroundStart";
import {LoadingCount} from "./js/player/LoadingCount.js";
import {BackGround} from "./js/runtime/BackGround.js";
import {DataStore} from "./js/base/DataStore.js";
import {Director} from "./js/Director.js";
import {Land} from "./js/runtime/Land.js";
import {Birds} from "./js/player/Birds.js";
import {BeginButton} from "./js/player/BeginButton";
import { Score } from "./js/player/Score.js";

import { Interview} from "./js/player/Interview.js";
import { Girl} from "./js/player/Girl.js";
import { Bottom} from "./js/player/Bottom.js";
import {Rule} from './js/runtime/Rule.js'


import { BackGroundExample } from "./js/runtime/BackgroundExample.js";

export class Main {

    constructor() {
        this.flag=false
        this.interviewFlag=true;//只注册一个介绍页
        this.canvas = wx.createCanvas();
        this.ctx = this.canvas.getContext('2d');
        this.dataStore = DataStore.getInstance();
        
        this.director = Director.getInstance();
        this.director.isGameOver = true;
        const loader = ResourceLoader.create();
        loader.onLoaded(map => this.onResourceFirstLoaded(map));
        this.loadingStep=0
        // this.man=null //选择的角色
       const getLoadingStep1= setInterval(() => {//获取加载进度
           this.loadingStep= loader.getLoadingStep()
           if(this.loadingStep>=100){
            this.loadingStep=100
            clearInterval(getLoadingStep1)
            
           }
           console.log(this.loadingStep)
        }, 20);
        
        
    }

    //创建背景音乐
    // createBackgroundMusic() {
    //     const bgm = wx.createInnerAudioContext();
    //     bgm.autoplay = true;
    //     bgm.loop = true;
    //     bgm.src = 'audios/bgm.mp3';
    // }
    

    onResourceFirstLoaded(map) {

        
        this.dataStore.canvas = this.canvas;
        this.dataStore.ctx = this.ctx;
        this.dataStore.res = map;
        this.director.isGameOver = true;
        this.dataStore
            .put('backgroundStart', BackGroundStart)
            .put('loadingCount',LoadingCount)
            // .put('background', BackGround)
            // .put('beginButton',BeginButton)
            .put('score',Score)
            .put('rule',Rule)

        this.dataStore.get('backgroundStart').draw(); //加载首页
        this.dataStore.get('backgroundStart').drawStartTit();  
        this.dataStore.get('backgroundStart').drawStartMan();  
         
        this.dataStore.get('loadingCount').drawLoading();  //加载loading
        this.dataStore.get('loadingCount').darwLoadingBar() ;


        // this.dataStore.get('loadingCount').loadingBar() ;
        // this.dataStore.get('background').draw();
        // this.dataStore.get('beginButton').draw();
        this.registerEvent();
    }

    init() {
        //首先重置游戏是没有结束的
        this.director.isGameOver = false;
        this.dataStore
            .put('foods',[])
            .put('background', BackGround)
            .put('land', Land)
            .put('birds',new Birds(Director.man))
            

        // this.registerEvent();

        //创建铅笔要在游戏逻辑运行之前
        this.director.run();
    }
    
    interview() {
        //游戏介绍
        this.dataStore
            .put('background', BackGround)
            .put('interview', Interview)
            
            .put('girl',Girl)
            .put('bottom',Bottom)
            .put('example',BackGroundExample)
        this.director.state='interview'
        this.director.interview()

    }

    registerEvent() {
        // this.canvas.addEventListener('touchstart', e => {
        //     //屏蔽掉JS的事件冒泡
        //     e.preventDefault();
        //     if (this.director.isGameOver) {
        //         console.log('游戏开始');
        //         this.init();
        //     } else {
        //         this.director.birdsEvent();
        //     }
        // });

        // wx.onTouchStart(() => {
        //     if (this.director.isGameOver) {
        //         console.log('游戏开始');
        //         this.init();
        //         this.director.birdsEvent();
        //     }
        // });
        wx.onTouchStart(() => {
            if (this.director.isGameOver) {
                // this.gameover()
                // this.director.birdsEvent();
            }
            if(this.interviewFlag==true){
                console.log('游戏介绍');
                this.interview();
                this.director.manEvent();
                this.director.bottomEvent();
                this.interviewFlag=false ;
                            
            }
            if(this.interviewFlag==false  && this.director.isGameOver &&Director.showExample){//进行游戏
                this.init()
                console.log('开始游戏')

                
                this.director.birdsEvent();
            }
        });
    }
}